<?php
namespace Home\Controller;
use Think\Controller;
header('content-type:text/html;charset=utf-8');
class LoginController extends Controller{
	//激活用户
	public function verify(){
		$where['id'] = $_GET['id'];
		$data['state'] = 1;
		$res = M('user')->where($where)->save($data);
		if($res){
			$this->success('激活成功！',U('Login/index'));
		}else{
			$this->error('激活失败，请联系管理员');
		}
	}
	//生成验证码的方法
	public function createCode(){

		$Verify = new \Think\Verify();
		//调整验证码的格式
		$Verify->codeSet = '0123456789';
		$Verify->imageW=100;
		$Verify->imageH=30;
		$Verify->fontSize=14;
		$Verify->length=4;
		$Verify->useNoise=false;
		$Verify->entry();
	}

	 public function index(){

	  $flag=I('get.flag');
	  $goodsid=I('get.goodsid');

	  $this->assign('flag',$flag);
	  $this->assign('goodsid',$goodsid);
      $img=M('img');
      $imgs=$img->where('state="已过期"')->select();

      $this->assign('lubn',$imgs);
      $this->assign('goodsid',$goodsid);
      $this->display();

    }


	//检测登录的方法
	public function checkLogin(){
		$code=I('post.code');
		//验证码的验证
		$verify=new \Think\Verify();

		$result=$verify->check($code);
		if(!$result){
			$this->error('验证码错误',U('Login/index'));
		}
		//接受用户提交的信息进行验证
		$user=M('User');
		//数据数组
		$arr['username']=I('post.username');
		//自己设计加MD5
		$arr['password']=md5(I('post.password'));
		$arr['_logic']='AND';
		$loginUser=$user->where($arr)->find();
		//判断用户是否激活
		if($loginUser && $loginUser['state']==0){
			$this->error('请在邮箱中激活用户');
		}
		$flag=0;
		$flag=I('post.flag');
		$goodsid=I('post.goodsid');



		switch ($flag) {
			// 用户从首页登录
			case 0:
					if($loginUser){
						//将用户登录信息写入SESSION
						$_SESSION['isLogin']=true;//登录标志
						$_SESSION['id']=$loginUser['id'];//登录id
						$_SESSION['username']=$loginUser['username'];;//登录名字
						$_SESSION['email']=$loginUser['email'];;//登录邮箱
						//验证通过
						$this->success('欢迎光临'.$loginUser['username'],U('Index/index'));

					}else{
						//验证没通过
						$this->error('用户名密码错误，请重新登录',U('Login/index'));
					}
				break;

			// 用户从购物车登录
			case 1:
					if($loginUser){
						//将用户登录信息写入SESSION
						$_SESSION['isLogin']=true;//登录标志
						$_SESSION['id']=$loginUser['id'];//登录id
						$_SESSION['username']=$loginUser['username'];;//登录名字
						$_SESSION['email']=$loginUser['email'];;//登录邮箱
						//验证通过
						$this->success('欢迎光临'.$loginUser['username'],''.__MODULE__.'/Goodsdetail/index/goodsid/'.$goodsid.'');

					}else{
						//验证没通过
						$this->error('用户名密码错误，请重新登录',''.__MODULE__.'/Login/index/flag/1/goodsid/'.$goodsid.'');
					}
			default:
				break;
		}

	}

	//退出方法
	public function logout(){
		//销毁session
		$_SESSION=array();
		setCookie('isLogin',null,time(),'/');
		setCookie('id',null,time(),'/');
		setCookie('username',null,time(),'/');
		setCookie('email',null,time(),'/');
		session_destroy();
		//跳转会登录页面
		$this->success('再见～',U('Index/index'));

	}


}